package com.dqhl.business.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dqhl.business.Base.BaseDialogActivity;
import com.dqhl.business.R;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.Dlog;
import com.dqhl.business.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * description: 添加分类
 * autour: guanluocang
 * date: 2016/11/4 16:10
 */
public class AddGoodsTypeActivity extends BaseDialogActivity implements View.OnClickListener {

    private TextView tv_cancel;
    private TextView tv_confim;
    private static final int RESULTCODE = 101;
    private EditText et_typeName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_gods_type);
        initView();
    }

    private void initView() {
        (tv_cancel) = (TextView) findViewById(R.id.tv_cancel);
        (tv_confim) = (TextView) findViewById(R.id.tv_confim);
        (et_typeName) = (EditText) findViewById(R.id.et_typeName);
        tv_confim.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_confim:
                doConfirm();
                break;
        }
    }

    private void doConfirm() {
        final String typeName = et_typeName.getText().toString().trim();
        if (TextUtils.isEmpty(typeName)) {
            toast("请填写分类名");
        } else {
            RequestParams params = new RequestParams(Config.management_add_goods_type);
            params.addBodyParameter("seller_id", user.getUser_id());
            params.addBodyParameter("name", typeName);
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Dlog.e(result);
                    int errCode = JsonUtils.getErrCode(result);
                    String errMsg = JsonUtils.getErrMsg(result);
                    String data = JsonUtils.getData(result);
                    if (0 == errCode) {
                        String id = JsonUtils.getInfo(data, "id");
                        Intent intent = new Intent();
                        intent.putExtra("typeName", typeName);
                        intent.putExtra("id", id);
                        setResult(RESULTCODE, intent);
                        finish();
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }


}
